package com.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.dto.Result;
import com.demo.dto.UserDTO;
import com.demo.entity.Blog;
import com.demo.service.IBlogService;
import com.demo.service.IUserService;
import com.demo.utils.SystemConstants;
import com.demo.utils.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog")
public class BolgController {

    @Resource
    private IBlogService blogService;

    @Resource
    private IUserService userService;

    /**
     * 发布帖子功能
     * @param blog
     * @return
     */
    @PostMapping
    public Result saveBolg(@RequestBody Blog blog){
        return blogService.saveBlog(blog);
    }

    /**
     * 查询帖子详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result queryBlogById(@PathVariable("id") Long id){
        return blogService.queryBlogById(id);
    }

    /**
     * 点赞帖子功能
     * @param id
     * @return
     */
    @PutMapping("like/{id}")
    public Result likeBlog(@PathVariable("id") Long id){
        return blogService.likeBlog(id);
    }

    /**
     *按点赞时间返回用户
     * @param id
     * @return
     */
    @GetMapping("/like/{id}")
    public Result queryBlogLikes(@PathVariable("id") Long id){
        return blogService.queryBlogLikes(id);
    }

    /**
     * 分页查询帖子
     * @param current
     * @param id
     * @return
     */
    @GetMapping("/of/user")
    public Result queryBlogByUserId(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam("id") Long id) {
        // 根据用户查询
        Page<Blog> page = blogService.query()
                .eq("user_id", id).page(new Page<>(current, SystemConstants.MAX_PAGE_SIZE));
        // 获取当前页数据
        List<Blog> records = page.getRecords();
        return Result.ok(records);
    }

    /**
     * 滚动分页查询
     * @param max
     * @param offset
     * @return
     */
    @GetMapping("of/follow")
    public Result queryBlogOfFollow(@RequestParam("lastId") Long max, @RequestParam(value = "offset",defaultValue = "0")Integer offset){
        return blogService.queryBlogOfFollow(max,offset);
    }
}
